﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices; // 用 DllImport 需用此 命名空间

namespace LEDShow
{
    //设备通讯参数
    public struct TDeviceParam
    {
        public ushort devType;
        public ushort comSpeed;
        public ushort comPort;
        public ushort comFlow;
        public ushort locPort;
        public ushort rmtPort;
        public ushort srcAddr;
        public ushort dstAddr;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
        public String rmtHost;
        public uint txTimeo;   //发送后等待应答时间 ====超时时间应为txTimeo*txRepeat
        public uint txRepeat;  //失败重发次数
        public uint txMovewin; //划动窗口
        public uint key;       //通讯密钥
        public int pkpLength;  //数据包大小

    }

    //设备控制参数
    public struct TSenderParam
    {
        public TDeviceParam devParam;
        public UInt32 wmHandle;
        public UInt32 wmMessage;
        public UInt32 wmLParam;
        public UInt32 notifyMode;
    }

    //设备应答消息参数
    public struct TNotifyParam
    {
        public ushort notify;
        public ushort command;
        public Int32 result;
        public Int32 status;
        TSenderParam param;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 512)]
        public byte[] buffer;
        UInt32 size;
    }

    //SYSTEMTIME型日期时间结构
    public struct TSystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMilliseconds;
    }

    //TimeStamp型日期时间结构
    public struct TTimeStamp
    {
        public Int32 time;
        public Int32 date;
    }

    public class CLEDSender
    {
        //通讯设备类型
        public readonly ushort DEVICE_TYPE_COM = 0;  //串口通讯
        public readonly ushort DEVICE_TYPE_UDP = 1;  //网络通讯
        public readonly ushort DEVICE_TYPE_485 = 2;  //485通讯

        //串口或者485通讯使用得通讯速度(波特率)
        public readonly ushort SBR_57600 = 0;
        public readonly ushort SBR_38400 = 1;
        public readonly ushort SBR_19200 = 2;
        public readonly ushort SBR_9600  = 3;

        //是否等待下位机应答，直接发送所有数据
        public readonly UInt32 NOTIFY_NONE  = 1;
        //是否阻塞方式；是则等到发送完成或者超时，才返回；否则立即返回
        public readonly UInt32 NOTIFY_BLOCK = 2;
        //是否将发送结果以Windows窗体消息方式送到调用得应用
        public readonly UInt32 NOTIFY_EVENT = 4;

        public readonly Int32 R_DEVICE_READY    = 0;
        public readonly Int32 R_DEVICE_INVALID  = -1;
        public readonly Int32 R_DEVICE_BUSY     = -2;
        public readonly Int32 R_FONTSET_INVALID = -3;
        public readonly Int32 R_DLL_INIT_IVALID = -4;
        public readonly Int32 R_IGNORE_RESPOND  = -5;

        //下位机应答标识
        public readonly ushort LM_RX_COMPLETE = 1;
        public readonly ushort LM_TX_COMPLETE = 2;
        public readonly ushort LM_RESPOND     = 3;
        public readonly ushort LM_TIMEOUT     = 4;
        public readonly ushort LM_NOTIFY      = 5;
        public readonly ushort LM_PARAM       = 6;
        public readonly ushort LM_TX_PROGRESS = 7;
        public readonly ushort LM_RX_PROGRESS = 8;
        public readonly ushort RESULT_FLASH = 0xff;

        public readonly Int32 NOTIFY_ROOT_UPDATE          = 0x00010001;  //在线更新控制卡程序
        public readonly Int32 NOTIFY_ROOT_UPDATE_ERROR    = 0x00020001;  //下载字库失败
        public readonly Int32 NOTIFY_ROOT_FONTSET         = 0x00010002;  //下载字库成功
        public readonly Int32 NOTIFY_ROOT_FONTSET_ERROR   = 0x00020002;  //下载字库失败
        public readonly Int32 NOTIFY_ROOT_DOWNLOAD        = 0x00010003;  //更新Flash播放节目
        public readonly Int32 NOTIFY_SET_PARAM            = 0x00010004;  //设置参数
        public readonly Int32 NOTIFY_SET_POWER_SCHEDULE   = 0x00010005;  //设置定时开关屏
        public readonly Int32 NOTIFY_SET_BRIGHT_SCHEDULE  = 0x00010006;  //设置定时亮度调节
        public readonly Int32 NOTIFY_SET_POWER_FLASH      = 0x0001000a;  //设置电源状态并保存到Flash
        public readonly Int32 NOTIFY_SET_BRIGHT_FLASH     = 0x0001000b;  //设置亮度并保存到Flash
        public readonly Int32 NOTIFY_GET_PLAY_BUFFER      = 0x00011001;  //读取控制卡显示内容

        //命令代码定义
        public readonly ushort PKC_RESPOND     = 3;
        public readonly ushort PKC_QUERY       = 4;
        public readonly ushort PKC_OVERFLOW    = 5;
        public readonly ushort PKC_ADJUST_TIME = 6;
        public readonly ushort PKC_GET_PARAM   = 7;
        public readonly ushort PKC_SET_PARAM   = 8;
        public readonly ushort PKC_GET_POWER   = 9;
        public readonly ushort PKC_SET_POWER   = 10;
        public readonly ushort PKC_GET_BRIGHT  = 11;
        public readonly ushort PKC_SET_BRIGHT  = 12;
        public readonly ushort PKC_GET_TEMPERATURE_HUMIDITY = 24;

        //电源开关参数值
        public readonly Int32 LED_POWER_ON  = 1;
        public readonly Int32 LED_POWER_OFF = 0;

        //Chapter和Leaf中，播放时间控制
        public readonly ushort WAIT_USE_TIME = 0;  //按照指定的时间长度播放，到时间就切到下一个
        public readonly ushort WAIT_CHILD    = 1;  //等待子项目的播放，如果到了指定的时间长度，而子项目还没有播完，则等待播完

        //整形TRUE/FALSE值
        public readonly Int32 V_FALSE = 0;
        public readonly Int32 V_TRUE  = 1;

        //显示屏基色类型
        public readonly Int32 COLOR_MODE_MONO       = 1;  //单色
        public readonly Int32 COLOR_MODE_DOUBLE     = 2;  //双色
        public readonly Int32 COLOR_MODE_FULL_16BIT = 3;  //16位全彩
        public readonly Int32 COLOR_MODE_FULL_32BIT = 4;  //32位全彩

        //显示数据命令
        public readonly Int32 ROOT_UPDATE       = 0x13;  //更新下位机程序
        public readonly Int32 ROOT_FONTSET      = 0x14;  //下载字库
        public readonly Int32 ROOT_PLAY         = 0x21;  //节目数据，保存到RAM，掉电丢失
        public readonly Int32 ROOT_DOWNLOAD     = 0x22;  //节目数据，保存到Flash
        public readonly Int32 ROOT_PLAY_CHAPTER = 0x23;  //插入或者更新某一节目
        public readonly Int32 ROOT_PLAY_REGION  = 0x25;  //插入或者更新某一区域/分区
        public readonly Int32 ROOT_PLAY_LEAF    = 0x27;  //插入或者更新某一页面
        public readonly Int32 ROOT_PLAY_OBJECT  = 0x29;  //插入或者更新某一对象

        public readonly Int32 ACTMODE_INSERT    = 0;     //插入操作
        public readonly Int32 ACTMODE_REPLACE   = 1;     //替换操作

        //RAM节目播放
        public readonly Int32 SURVIVE_ALWAYS = -1;

        //Windows字体类型定义
        public readonly Int32 WFS_NONE      = 0x0;   //普通样式
        public readonly Int32 WFS_BOLD      = 0x01;  //粗体
        public readonly Int32 WFS_ITALIC    = 0x02;  //斜体
        public readonly Int32 WFS_UNDERLINE = 0x04;  //下划线
        public readonly Int32 WFS_STRIKEOUT = 0x08;  //删除线

        //内码文字大小
        public readonly Int32 FONT_SET_16 = 0;      //16点阵字符
        public readonly Int32 FONT_SET_24 = 1;      //24点阵字符
  
        //正计时、倒计时type参数
        public readonly Int32 CT_COUNTUP   = 0;      //正计时
        public readonly Int32 CT_COUNTDOWN = 1;      //倒计时
        public readonly Int32 CT_COUNTUP_EX = 2;     //倒计时
        public readonly Int32 CT_COUNTDOWN_EX = 3;   //倒计时
        
        //正计时、倒计时format参数
        public readonly Int32 CF_HNS    = 0;      //时分秒（相对值）
        public readonly Int32 CF_HN     = 1;      //时分（相对值）
        public readonly Int32 CF_NS     = 2;      //分秒（相对值）
        public readonly Int32 CF_H      = 3;      //时（相对值）
        public readonly Int32 CF_N      = 4;      //分（相对值）
        public readonly Int32 CF_S      = 5;      //秒（相对值）
        public readonly Int32 CF_DAY    = 6;      //天数（绝对数量）
        public readonly Int32 CF_HOUR   = 7;      //小时数（绝对数量）
        public readonly Int32 CF_MINUTE = 8;     //分钟数（绝对数量）
        public readonly Int32 CF_SECOND = 9;      //秒数（绝对数量）

        //模拟时钟边框形状
        public readonly Int32 SHAPE_RECTANGLE = 0;      //方形
        public readonly Int32 SHAPE_ROUNDRECT = 1;      //圆角方形
        public readonly Int32 SHAPE_CIRCLE    = 2;      //圆形

        //节目播放计划一周有效日期定义
        public readonly ushort CS_SUN = 1;
        public readonly ushort CS_MON = 2;
        public readonly ushort CS_TUE = 4;
        public readonly ushort CS_WED = 8;
        public readonly ushort CS_THU = 16;
        public readonly ushort CS_FRI = 32;
        public readonly ushort CS_SAT = 64;

        ////////////////////////////////////////////////////////////////////////////////////////////
        //接口函数导入
        ////////////////////////////////////////////////////////////////////////////////////////////

        //动态链接库初始化
        [DllImport("LEDSender2010.dll")]
        private static extern int LED_Startup();

        //动态链接库销毁
        [DllImport("LEDSender2010.dll")]
        private static extern int LED_Cleanup();


        //读取控制卡当前显示内容
        [DllImport("LEDSender2010.dll")]
        private static extern int LED_GetPlayContent(ref TSenderParam param);
        //预览显示内容
        [DllImport("LEDSender2010.dll")]
        private static extern void LED_PreviewFile(Int32 width, Int32 height, string previewfile);
 
        //获取控制卡应答结果的数据
        [DllImport("LEDSender2010.dll")]
        private static extern int LED_GetNotifyParam(ref TNotifyParam param, Int32 index);

        //获取控制卡应答结果的数据，并将附加数据保存到指定文件中
        [DllImport("LEDSender2010.dll")]
        private static extern int LED_GetNotifyParam_BufferToFile(ref TNotifyParam param, String filename, int index); 

        public int Do_LED_Startup()
        {
            return LED_Startup();
        } 
        public int Do_LED_GetPlayContent(ref TSenderParam param)
        {
            return LED_GetPlayContent(ref param);
        }

        public int Do_LED_GetNotifyParam_BufferToFile(ref TNotifyParam param, String filename, Int32 index)
        {
            return LED_GetNotifyParam_BufferToFile(ref param, filename, index);
        }
        public void Do_LED_PreviewFile(Int32 width, Int32 height, string previewfile)
        {
            LED_PreviewFile(width, height, previewfile);
        }
    }
}
